﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace FoundationLib.Common.Collections
{
	public class GroupedList<TKey, TElement>
	{
		private List<GroupedListItem<TKey, TElement>> items;

		public GroupedList()
		{
			Contract.Ensures(this.items != null);
			this.items = new List<GroupedListItem<TKey, TElement>>();
		}

		public void Add(TKey key, TElement element)
		{
			GroupedListItem<TKey, TElement> item = this.items.Where(x => x.Key.Equals(key)).FirstOrDefault();
			if (item != null)
			{
				item.Add(element);
			}
		}

		public void Remove(TElement element)
		{
			IEnumerable<GroupedListItem<TKey, TElement>> items = this.items.Where(x => x.Contains(element));
			foreach (GroupedListItem<TKey, TElement> item in items)
				item.Remove(element);
		}
	}
}
